package com.api.gbhg.liuwenbo.version2.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.dao.DataBaseUtil2;
import com.api.gbhg.liuwenbo.version2.dao.NonCentralTreeDBUtil2;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 各独立预算单位收支预算明细表
 * @Author lwb
 * @Date
 */
@Path("gb/BudgetSectionIncome2")
public class BudgetSectionIncomeApi2{

    @POST
    @Path("/getBudgetIncomeList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBudgetIncomeList(Map<String,Object> param){
        Map msgMap = new HashMap<>();

        if (param == null ||!param.containsKey("nf") || !param.containsKey("dw") ||!param.containsKey("jz")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String, Object> paramFormat = paramFormat(param);

        List<BudgetSectionIncome2> czbksrData = null;
        List<BudgetSectionIncome2> fczbksrData = null;
        try {
            czbksrData = getCzbksrData(paramFormat);
            fczbksrData = getFczbksrData(paramFormat);
        }catch (Exception e){
            e.printStackTrace();
            msgMap.put("code","500");
            msgMap.put("msg","程序异常，请联系管理员！"+e.getMessage());
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("1",czbksrData);
        map.put("2",fczbksrData);
        map.put("dw", DataBaseUtil2.getSubcompanyNameById(paramFormat.get("dw").toString()));

        msgMap.put("code","200");
        msgMap.put("msg","");
        msgMap.put("data",map);
        return JSON.toJSONString(msgMap);
    }

    /**
     * 参数转换
     * @param param
     * @return
     */
    public Map<String,Object> paramFormat(Map<String,Object> param){
        String dw = (!param.containsKey("key") && param.get("dw").equals(""))? DataBaseUtil2.getGonBeiSubCompany(): param.get("dw").toString();
        Integer nf = param.get("nf").equals("")? LocalDate.now().getYear():Integer.parseInt(param.get("nf").toString());
        String jz = param.get("jz").equals("")?"yqndyjjzje":param.get("jz").toString();

        param.put("dw",dw);
        param.put("nf",nf);
        param.put("jz",jz);
        return param;
    }

    /**
     * 获取地方财政同意报销金额
     * @param zcqd
     *          0和1 0为财政拨款 1为非财政拨款
     * @param ysly
     *          0和1 0为当年预算 1为上年结转
     * @param kmjxj
     *          0和1 分别对应科目级和科目下级，在财政收入的层级里0就是第一级，非第一级的都为1
     * @param dygnkmbid
     *          当kmjxj为0时为uf_zyfzbksr_km表的id，1则是uf_zyfzbksr_xj表的id
     * @param dw
     *          单位
     * @param nf
     *          年份
     * @return
     */
    public String getDfcz_tybxje(String zcqd,String ysly,String kmjxj,String dygnkmbid,String dw,String nf){
        RecordSet rs = new RecordSet();
        String sql = "select ISNULL(SUM(tybxje-isnull(cxje,0)),0) AS tybxje  from uf_bxjl_kj_dt1 where zcqd="+zcqd+" and ysly="+ysly+"  and kmjxj="+kmjxj+" and dygnkmbid='"+dygnkmbid+"' and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw="+dw+" and ysnd="+nf+") ";
        rs.executeQuery(sql);
        if (rs.next()){
            if (rs.getString("tybxje") == null || rs.getString("tybxje").equals(""))
                return "0";
            else
                return rs.getString("tybxje");
        }else{
            return "0";
        }
    }

    /**
     * 获取财政拨款收入数据
     * @param param
     * @return
     */
    public List<BudgetSectionIncome2> getCzbksrData(Map<String,Object> param){

        List<BudgetSectionIncome2> listBean = new ArrayList<>();

        String nf = param.get("nf").toString();
        String dw = param.get("dw").toString();
        String jz = param.get("jz").toString();
        String sql = "select id,gnkmmc,kmdm,"+jz+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr,dw from uf_zyczbksr_km where srnf = "+nf;
        if(!dw.equals("") && param.containsKey("dw")){
            sql += " and dw ="+dw+" ";
        }
        RecordSet rs = new RecordSet();
        System.err.println("获取财政拨款收入数据 "+sql);
        rs.executeQuery(sql);
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setDwId(rs.getString("dw"));
            bean.setDwName(getDwName(rs.getString("dw")));
            bean.setId(rs.getString("id"));
            bean.setDataid(rs.getString("id"));
            bean.setKmjxj(0);
            bean.setKmmc(rs.getString("kmdm"));
            bean.setSnjz(rs.getString(jz));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jz)).add(new BigDecimal(rs.getString("yjsr")))));

            //List<BudgetSectionIncome2> xjList = getCzbksrEjData(rs.getString("id"), jz,dw,nf);
            List<BudgetSectionIncome2> xjList = getCzbksrEjData(rs.getString("id"), jz,rs.getString("dw"),nf);
            if (xjList.size()>0){
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                for (BudgetSectionIncome2 beans:xjList){
                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                bean.setSnjzyszxl(snjzyszxl.toString());
                bean.setBnsryszxl(bnsryszxl.toString());
                bean.setHj4(snjzyszxl.add(bnsryszxl).toString());

                bean.setXj(xjList);
            }else{
                //已到最下级  取自身值
                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                String snbxje = getDfcz_tybxje("0", "1", "0", rs.getString("id"), rs.getString("dw"), nf);
                bean.setSnjzyszc(snbxje);
                String bnbxje = getDfcz_tybxje("0", "0", "0", rs.getString("id"), rs.getString("dw"), nf);
                bean.setBnsryszc(bnbxje);
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                BigDecimal snzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                    snzxl = new BigDecimal(bean.getSnjzyszc()).divide(new BigDecimal(bean.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setSnjzyszxl(snzxl.toString());

                BigDecimal bnzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                    bnzxl = new BigDecimal(bean.getBnsryszc()).divide(new BigDecimal(bean.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setBnsryszxl(bnzxl.toString());
                bean.setHj4(String.valueOf(snzxl.add(bnzxl)));
            }
            bean.setName(rs.getString("gnkmmc"));
            listBean.add(bean);
        }
        return listBean;
    }

    public String getDwName(String dwid){
        RecordSet rs = new RecordSet();
        rs.execute("select subcompanyname from HrmSubCompany where id="+dwid);
        rs.next();
        return rs.getString("subcompanyname");
    }
    /**
     * 获取非财政拨款收入数据
     * @param param
     * @return
     */
    public List<BudgetSectionIncome2> getFczbksrData(Map<String,Object> param){
        List<BudgetSectionIncome2> listBean = new ArrayList<>();

        Map<String, Object> srysfl = NonCentralTreeDBUtil2.getSrysfl();

        String nf = param.get("nf").toString();
        String dw = param.get("dw").toString();
        String jz = param.get("jz").toString();
        String sql = "select id,szfl,"+jz+",yjsr,snjzhpje,yhpje,yqndyjjzje,yssrfl,sjsr,dw from uf_fzysr where nf = "+nf+" ";
        if(!dw.equals("") && param.containsKey("dw")){
            sql += " and dw ="+dw+" ";
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setName(srysfl.get(rs.getString("szfl")).toString());
            bean.setId(rs.getString("szfl"));
            bean.setDwId(rs.getString("dw"));
            bean.setDwName(getDwName(rs.getString("dw")));

            //List<BudgetSectionIncome2> xjList = getFczbksrXm(rs.getString("id"), jz,rs.getString("yssrfl"),dw,nf);
            List<BudgetSectionIncome2> xjList = getFczbksrXm(rs.getString("id"), jz,rs.getString("yssrfl"),rs.getString("dw"),nf);
            if (xjList.size()>0){
//              本级收入
                BudgetSectionIncome2 bj = new BudgetSectionIncome2();
                bj.setKmmc("本级收入");
                bj.setId(rs.getString("szfl"));
                bj.setSnjz(rs.getString(jz));
                bj.setBnsr(rs.getString("yjsr"));
                bj.setHj(String.valueOf(new BigDecimal(rs.getString(jz)).add(new BigDecimal(rs.getString("yjsr")))));
                if (rs.getString("yssrfl").equals("5") || rs.getString("yssrfl").equals("8")) {
                    bj.setSnjzysap(rs.getString("snjzhpje"));
                    bj.setBnsrysap(rs.getString("yhpje"));
                    bj.setHj1(String.valueOf(new BigDecimal(bj.getSnjzysap()).add(new BigDecimal(bj.getBnsrysap()))));
                    bj.setSnjzwap(rs.getString("yqndyjjzje"));
                    bj.setBnsrwap(rs.getString("yjsr"));
                    bj.setHj2(String.valueOf(new BigDecimal(bj.getSnjzwap()).add(new BigDecimal(bj.getBnsrwap()))));

                    String snzc = getFzycz_tybxje("1", "1", rs.getString("yssrfl"), rs.getString("id"), rs.getString("dw"), nf, 0);
                    bj.setSnjzyszc(snzc);
                    String bnzc = getFzycz_tybxje("1", "0", rs.getString("yssrfl"), rs.getString("id"), rs.getString("dw"), nf, 0);
                    bj.setBnsryszc(bnzc);
                    bj.setHj3(String.valueOf(new BigDecimal(snzc).add(new BigDecimal(bnzc))));

                    BigDecimal snzxl = new BigDecimal("0");
                    if (new BigDecimal(bj.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                        snzxl = new BigDecimal(bj.getSnjzyszc()).divide(new BigDecimal(bj.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);
                    bj.setSnjzyszxl(snzxl.toString());

                    BigDecimal bnzxl = new BigDecimal("0");
                    if (new BigDecimal(bj.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                        bnzxl = new BigDecimal(bj.getBnsryszc()).divide(new BigDecimal(bj.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);
                    bj.setBnsryszxl(bnzxl.toString());
                    bj.setHj4(String.valueOf(snzxl.add(bnzxl)));

                    bj.setBnsjsr(rs.getString("sjsr"));
                }else{
                    bj.setSnjzysap("0");
                    bj.setBnsrysap("0");
                    bj.setHj1("0");
                    bj.setSnjzwap("0");
                    bj.setBnsrwap("0");
                    bj.setHj2("0");

                    bj.setSnjzyszc("0");
                    bj.setBnsryszc("0");
                    bj.setHj3("0");
                    bj.setSnjzyszxl("0");
                    bj.setBnsryszxl("0");
                    bj.setHj4("0");

                    bj.setBnsjsr("0");
                }

                List<BudgetSectionIncome2> bjList = new ArrayList<>();
                bjList.add(bj);

                bjList.addAll(xjList);
                bean.setXj(bjList);
            }

            listBean.add(bean);
        }
        return listBean;
    }

    //注释掉只为观看起来方便 已有重载方法
//    /**
//     * 获取中央科目二级数据
//     * @param kmId
//     * @return
//     */
//    public List<BudgetSectionIncome2> getCzbksrEjData(String kmId, String jzField){
//        RecordSet rs = new RecordSet();
//        String sql = "select id,srzx,"+jzField+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr from uf_zyczbksr_xj where szgnkm = "+kmId+" and sjzxsr is null";
//        rs.executeQuery(sql);
//        List<BudgetSectionIncome2> list = new ArrayList<>();
//        while (rs.next()){
//            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
//            bean.setKmmc(rs.getString("srzx"));
//            bean.setSnjz(rs.getString(jzField));
//            bean.setBnsr(rs.getString("yjsr"));
//            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));
//
//            List<BudgetSectionIncome2> xjList = recursionXj(rs.getString("id"), jzField);
//            if (xjList.size()>0){
//                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
//                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
//                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
//                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
//                for (BudgetSectionIncome2 beans:xjList){
//                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
//                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));
//
//                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
//                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));
//                }
//                bean.setSnjzysap(snjzysap.toString());
//                bean.setBnsrysap(bnsrysap.toString());
//                bean.setHj1(snjzysap.add(bnsrysap).toString());
//
//                bean.setSnjzwap(snjzwap.toString());
//                bean.setBnsrwap(bnsrwap.toString());
//                bean.setHj2(snjzwap.add(bnsrwap).toString());
//
//                bean.setXj(xjList);
//            }else{
//                //已到最下级  取值
//                bean.setSnjzysap(rs.getString("snjzhpje"));
//                bean.setBnsrysap(rs.getString("yhpje"));
//                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));
//
//                bean.setSnjzwap(rs.getString("yqndyjjzje"));
//                bean.setBnsrwap(rs.getString("yjsr"));
//                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));
//            }
//
//            list.add(bean);
//        }
//        return list;
//    }

    /**
     * 获取中央科目二级数据
     * @param kmId
     * @return
     */
    public List<BudgetSectionIncome2> getCzbksrEjData(String kmId, String jzField,String dw,String nf){
        RecordSet rs = new RecordSet();
        String sql = "select id,srzx,"+jzField+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr from uf_zyczbksr_xj where szgnkm = "+kmId+" and sjzxsr is null";
        //System.err.println("获取财政拨款下级收入数据："+sql);
        rs.executeQuery(sql);
        List<BudgetSectionIncome2> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setId(rs.getString("id"));
            bean.setKmjxj(1);
            bean.setKmmc(rs.getString("srzx"));
            bean.setSnjz(rs.getString(jzField));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));

            List<BudgetSectionIncome2> xjList = recursionXj(rs.getString("id"), jzField,dw,nf);
            if (xjList.size()>0){
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                List<String> dataidBj = new ArrayList<>();//记录本级
                List<String> dataidXj = new ArrayList<>();//记录下级
                for (BudgetSectionIncome2 beans:xjList){
                    dataidBj.add(beans.getId());
                    if (beans.getDataid() != null && !beans.getDataid().isEmpty()){
                        dataidXj.add(beans.getDataid());
                    }
                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                String dataid = "";
                if (dataidXj.size()>0){
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",")+","+VersionFlowUtil.joinStringBySplit(dataidXj,",");
                }else{
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",");
                }
                bean.setDataid(dataid);

                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                bean.setSnjzyszxl(snjzyszxl.toString());
                bean.setBnsryszxl(bnsryszxl.toString());
                bean.setHj4(snjzyszxl.add(bnsryszxl).toString());

                bean.setXj(xjList);
            }else{
                //已到最下级  取值
                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                String snbxje = getDfcz_tybxje("0", "1", "1", rs.getString("id"), dw, nf);
                bean.setSnjzyszc(snbxje);
                String bnbxje = getDfcz_tybxje("0", "0", "1", rs.getString("id"), dw, nf);
                bean.setBnsryszc(bnbxje);
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                BigDecimal snzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                    snzxl = new BigDecimal(bean.getSnjzyszc()).divide(new BigDecimal(bean.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setSnjzyszxl(snzxl.toString());

                BigDecimal bnzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                    bnzxl = new BigDecimal(bean.getBnsryszc()).divide(new BigDecimal(bean.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);

                bean.setBnsryszxl(bnzxl.toString());
                bean.setHj4(String.valueOf(snzxl.add(bnzxl)));
            }

            list.add(bean);
        }
        return list;
    }

    /**
     * 获取非中央项目数据
     * @param id
     * @param jzField
     * @return
     */
    public List<BudgetSectionIncome2> getFczbksrXm(String id,String jzField,String yssrfl,String dw,String nf){
        RecordSet rs = new RecordSet();
        String sql = "select id,"+jzField+",yjsr,xmmc,snjzhpje,yhpje,yqndyjjzje,sjsr from uf_fzysrxm where szfl = "+id+"";
        rs.executeQuery(sql);
        List<BudgetSectionIncome2> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setKmmc(rs.getString("xmmc"));
            bean.setId(rs.getString("id"));
            bean.setSnjz(rs.getString(jzField));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));
            if (yssrfl.equals("5") || yssrfl.equals("8")){
                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));
                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                String snzc = getFzycz_tybxje("1", "1", yssrfl, rs.getString("id"), dw, nf, 1);
                bean.setSnjzyszc(snzc);
                String bnzc = getFzycz_tybxje("1", "0", yssrfl, rs.getString("id"), dw, nf, 1);
                bean.setBnsryszc(bnzc);
                bean.setHj3(String.valueOf(new BigDecimal(snzc).add(new BigDecimal(bnzc))));

                BigDecimal snzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                    snzxl = new BigDecimal(bean.getSnjzyszc()).divide(new BigDecimal(bean.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setSnjzyszxl(snzxl.toString());

                BigDecimal bnzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                    bnzxl = new BigDecimal(bean.getBnsryszc()).divide(new BigDecimal(bean.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setBnsryszxl(bnzxl.toString());
                bean.setHj4(String.valueOf(snzxl.add(bnzxl)));

                bean.setBnsjsr(rs.getString("sjsr"));
            }else{
                bean.setSnjzysap("0");
                bean.setBnsrysap("0");
                bean.setHj1("0");
                bean.setSnjzwap("0");
                bean.setBnsrwap("0");
                bean.setHj2("0");

                bean.setSnjzyszc("0");
                bean.setBnsryszc("0");
                bean.setHj3("0");
                bean.setSnjzyszxl("0");
                bean.setBnsryszxl("0");
                bean.setHj4("0");

                bean.setBnsjsr("0");
            }


            list.add(bean);
        }
        return list;
    }


    /**
     * 获取地方财政同意报销金额
     * @param zcqd
     *          0和1 0为财政拨款 1为非财政拨款
     * @param ysly
     *          0和1 0为当年预算 1为上年结转
     * @param fzysrfl
     *          非中央收入分类表的id，对应了uf_srysfl表的id和uf_fzysrxm的yssrfl字段
     * @param zcdfczbkxm
     *          非中央拨款项目，当fzysrfl为5或8的时候有值，对应了uf_fzysrxm表的id
     * @param dw
     *          单位
     * @param nf
     *          年份
     * @param ix
     *          标识 0为上层 1下层项目
     * @return
     */
    public String getFzycz_tybxje(String zcqd,String ysly,String fzysrfl,String zcdfczbkxm,String dw,String nf,int ix){
        RecordSet rs = new RecordSet();
        String sql = "";
        if (ix==0){
            sql = "select  sum(tybxje) tybxje from uf_bxjl_kj_dt1 where zcqd="+zcqd+" and ysly="+ysly+"  and fzysrfl="+fzysrfl+"  and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw="+dw+" and ysnd="+nf+") ";
        }else{
            sql = "select  sum(tybxje) tybxje from uf_bxjl_kj_dt1 where zcqd="+zcqd+" and ysly="+ysly+"  and fzysrfl="+fzysrfl+" and zcdfczbkxm ='"+zcdfczbkxm+"'  and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw="+dw+" and ysnd="+nf+") ";
        }

        rs.executeQuery(sql);
        if (rs.next()){
            if (rs.getString("tybxje") == null || rs.getString("tybxje").equals(""))
                return "0";
            else
                return rs.getString("tybxje");
        }else{
            return "0";
        }
    }

    //注释掉只为观看起来方便些
//    /**
//     * 获取所有下级信息
//     * @param id
//     * @param jzField
//     * @return
//     */
//    public List<BudgetSectionIncome2> recursionXj(String id,String jzField){
//        RecordSet rs = new RecordSet();
//        String sql = "select id,srzx,"+jzField+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr from uf_zyczbksr_xj where sjzxsr = "+id+"";
//        rs.executeQuery(sql);
//        List<BudgetSectionIncome2> list = new ArrayList<>();
//        while (rs.next()){
//            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
//            bean.setKmmc(rs.getString("srzx"));
//            bean.setSnjz(rs.getString(jzField));
//            bean.setBnsr(rs.getString("yjsr"));
//            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));
//            bean.setSnjzysap("0");
//            bean.setBnsrysap("0");
//            bean.setSnjzwap("0");
//            bean.setBnsrwap("0");
//
//            List<BudgetSectionIncome2> xjList = recursionXj(rs.getString("id"), jzField);
//            if (xjList.size()>0) {
//                BigDecimal snjzysap = BigDecimal.ZERO; //上年结转预算安排
//                BigDecimal bnsrysap = BigDecimal.ZERO; //本年收入预算安排
//                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
//                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
//                for (BudgetSectionIncome2 beans:xjList){
//                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
//                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));
//
//                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
//                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));
//                }
//                bean.setSnjzysap(snjzysap.toString());
//                bean.setBnsrysap(bnsrysap.toString());
//                bean.setHj1(snjzysap.add(bnsrysap).toString());
//
//                bean.setSnjzwap(snjzwap.toString());
//                bean.setBnsrwap(bnsrwap.toString());
//                bean.setHj2(snjzwap.add(bnsrwap).toString());
//
//                bean.setXj(xjList);
//            }else{
//                //已到最下级  取值
//                bean.setSnjzysap(rs.getString("snjzhpje"));
//                bean.setBnsrysap(rs.getString("yhpje"));
//                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));
//
//                bean.setSnjzwap(rs.getString("yqndyjjzje"));
//                bean.setBnsrwap(rs.getString("yjsr"));
//                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));
//
//            }
//
//            list.add(bean);
//        }
//        return list;
//    }

    /**
     * 获取所有下级信息
     * @param id
     * @param jzField
     * @return
     */
    public List<BudgetSectionIncome2> recursionXj(String id,String jzField,String dw,String nf){
        RecordSet rs = new RecordSet();
        String sql = "select id,srzx,"+jzField+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr from uf_zyczbksr_xj where sjzxsr = "+id+"";
        rs.executeQuery(sql);
        List<BudgetSectionIncome2> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome2 bean = new BudgetSectionIncome2();
            bean.setKmjxj(1);
            bean.setId(rs.getString("id"));
            bean.setKmmc(rs.getString("srzx"));
            bean.setSnjz(rs.getString(jzField));
            bean.setBnsr(rs.getString("yjsr"));
            bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));
            bean.setSnjzysap("0");
            bean.setBnsrysap("0");
            bean.setSnjzwap("0");
            bean.setBnsrwap("0");
            bean.setSnjzyszc("0");
            bean.setBnsryszc("0");
            bean.setSnjzyszxl("0");
            bean.setBnsryszxl("0");

            List<BudgetSectionIncome2> xjList = recursionXj(rs.getString("id"), jzField,dw,nf);
            if (xjList.size()>0) {
                BigDecimal snjzysap = BigDecimal.ZERO; //上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO; //本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                List<String> dataidBj = new ArrayList<>();//记录本级
                List<String> dataidXj = new ArrayList<>();//记录下级
                for (BudgetSectionIncome2 beans:xjList){
                    dataidBj.add(beans.getId());
                    if (beans.getDataid() != null && !beans.getDataid().isEmpty()){
                        dataidXj.add(beans.getDataid());
                    }
                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                String dataid = "";
                if (dataidXj.size()>0){
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",")+","+VersionFlowUtil.joinStringBySplit(dataidXj,",");
                }else{
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",");
                }
                bean.setDataid(dataid);

                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                bean.setSnjzyszxl(snjzyszxl.toString());
                bean.setBnsryszxl(bnsryszxl.toString());
                bean.setHj4(snjzyszxl.add(bnsryszxl).toString());

                bean.setXj(xjList);
            }else{
                //已到最下级  取值
                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                String snbxje = getDfcz_tybxje("0", "1", "1", rs.getString("id"), dw, nf);
                bean.setSnjzyszc(snbxje);
                String bnbxje = getDfcz_tybxje("0", "0", "1", rs.getString("id"), dw, nf);
                bean.setBnsryszc(bnbxje);
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                BigDecimal snzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getSnjzysap()).compareTo(new BigDecimal("0"))>0)
                if (!bean.getSnjzysap().equals("0"))
                    snzxl = new BigDecimal(bean.getSnjzyszc()).divide(new BigDecimal(bean.getSnjzysap()), 2, BigDecimal.ROUND_HALF_UP);

                bean.setSnjzyszxl(snzxl.toString());

                BigDecimal bnzxl = new BigDecimal("0");
                if (new BigDecimal(bean.getBnsrysap()).compareTo(new BigDecimal("0"))>0)
                    bnzxl = new BigDecimal(bean.getBnsryszc()).divide(new BigDecimal(bean.getBnsrysap()), 2, BigDecimal.ROUND_HALF_UP);
                bean.setBnsryszxl(bnzxl.toString());
                bean.setHj4(String.valueOf(snzxl.add(bnzxl)));

            }

            list.add(bean);
        }
        return list;
    }

    /**
     * 上年结转预算安排/本年收入预算安排 金额字段
     * @param nd 本年0 上年1
     * @param id 数据id
     * @param ix 科目 0/下级 1
     * @return
     */
    public String getSnjzysap(int nd,String id,int ix){
        RecordSet rs = new RecordSet();
        String sql = "";
        return null;
    }
}
